import sbt.IO._
import sbt.Keys._
import sbt._

object BuildSettings {
  val packageDist = TaskKey[Unit]("package-dist")
  val dist = TaskKey[Unit]("dist")

  val buildSettings: Seq[Setting[_]] = Defaults.defaultSettings ++ Seq[Setting[_]](
    organization := "im.yagni",
    version := "0.2.32",
    scalaVersion := "2.10.4",
    scalacOptions ++= Seq("-deprecation", "-optimise", "-explaintypes"),
//    crossScalaVersions := Seq(/*"2.9.2", */"2.10.1"),
    javaOptions ++= Seq("-Xmx256m", "-Xss4m", "-server"),
  
    parallelExecution := true,
    parallelExecution in Test := true,
    logBuffered := false,

    testOptions in Test += Tests.Cleanup( (loader: java.lang.ClassLoader) => {
      loader.loadClass("im.yagni.acceptance.Stop").newInstance()
    }),

//    testOptions in Test ++= Seq(Tests.Argument("junitxml", "html", "console")),

    packageDist <<= (baseDirectory, crossTarget, version, packageBin in Compile, packageDoc in Compile, packageSrc in Compile) map {
      (theBase, targetDir, theVersion, jarFile, docFile, srcFile) ⇒

        val docs = (theBase / "docs" * "*")
        val distribution = (theBase / "distribution" * "*")
        val docEntries = docs x flat
        val distributionEntries = distribution x flat

        val distPaths = (jarFile +++ docFile +++ srcFile) x relativeTo(targetDir)
        zip(distPaths ++ docEntries ++ distributionEntries, (targetDir / ("DriveBy-" + theVersion + ".zip")))
    },
    dist <<= Seq(packageBin in Compile, packageDoc in Compile, packageSrc in Compile, packageDist).dependOn)
}

object Resolvers {
  val sonatypeRepo = "Sonatype Repo" at "http://oss.sonatype.org/content/groups/public/"
}

object Dependencies {
  val flyScalaVersion = "2.1.5"
  val jettyVersion = "8.1.14.v20131031"
//  val nscalaTimeVersion = "0.4.2"
  val nscalaTimeVersion = "0.2.0"
  val scalaTestVersion = "2.1.3"
  val seleniumVersion = "2.44.0"
  val specs2Version = "1.14"
  val phantomjsDriverVersion = "1.1.0"

  val jetty = "org.eclipse.jetty" % "jetty-webapp" % jettyVersion
  val nscalaTime = "com.github.nscala-time" %% "nscala-time" % nscalaTimeVersion
  val phantomjsDriver = "com.github.detro.ghostdriver" % "phantomjsdriver" % phantomjsDriverVersion exclude("org.seleniumhq.selenium", "selenium-server")

    val specs2 = Seq(
    "org.specs2" %% "specs2" % specs2Version
    //"org.mockito" % "mockito-all" % "1.8.5" % "test->default"
    //"com.borachio" %% "borachio-specs2-support" % "1.3" % "test"
	)

//  val scalaTest = "org.scalatest" %% "scalatest" % scalaTestVersion
  val scalaTest = "org.scalatest" % "scalatest_2.10" % scalaTestVersion

  val selenium = Seq(
//    "org.seleniumhq.selenium" % "selenium-server" % seleniumVersion,
    "org.seleniumhq.selenium" % "selenium-firefox-driver" % seleniumVersion,
    "org.seleniumhq.selenium" % "selenium-ie-driver" % seleniumVersion,
    "org.seleniumhq.selenium" % "selenium-chrome-driver" % seleniumVersion
//    "org.seleniumhq.selenium" % "selenium-htmlunit-driver" % seleniumVersion
  )

  val flyScala = "com.flyobjectspace" %% "flyscala" % flyScalaVersion /*withSources()*/
  val pegdown = "org.pegdown" % "pegdown" % "1.0.2" % "test"
  val junit = "junit" % "junit" % "4.9"// % "test"
}

/* see http://www.scala-sbt.org/using_sonatype.html and http://www.cakesolutions.net/teamblogs/2012/01/28/publishing-sbt-projects-to-nexus/
 * Instructions from sonatype: https://issues.sonatype.org/browse/OSSRH-2841?focusedCommentId=150049#comment-150049
 * Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
 * Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
 * Promote staged artifacts into repository 'Releases'
 * Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public
 * Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging
 */
object Publishing {
  def publishSettings: Seq[Setting[_]] = Seq(

    publishMavenStyle := true,
    publishArtifact in Test := false,
    pomIncludeRepository := { x ⇒ false },

    publishTo <<= version { v: String ⇒
      val nexus = "https://oss.sonatype.org/"
      if (v.trim.endsWith("SNAPSHOT"))
        Some("snapshots" at nexus + "content/repositories/snapshots")
      else
        Some("releases" at nexus + "service/local/staging/deploy/maven2")
    },

    pomExtra := (
      <licenses>
        <license>
          <name>MIT License</name>
          <url>http://www.opensource.org/licenses/mit-license/</url>
          <distribution>repo</distribution>
        </license>
      </licenses>
      <scm>
        <url>http://driveby.googlecode.com/svn/trunk/</url>
      </scm>
      <url>http://code.google.com/p/driveby/</url>
      <developers>
        <developer>
          <id>alltonp</id>
          <name>Paul Allton</name>
          <email>paulallton [at] gmail [dot] com</email>
          <organization>Yagni Ltd</organization>
        </developer>
      </developers>))
}

object SbtBuild extends Build {
  import BuildSettings._
  import Dependencies._
  import Publishing._
  import Resolvers._

  lazy val driveBy = Project(
    "DriveBy",
    file("."),
    settings = buildSettings ++ publishSettings ++ Seq(resolvers := Seq(sonatypeRepo, Classpaths.typesafeResolver),
      libraryDependencies ++= specs2 ++ selenium ++ Seq(flyScala, jetty, nscalaTime, junit, pegdown, scalaTest, phantomjsDriver)))
}
